// 交互js
// 显示编辑框
function show_addtxt() {
  // alert(index)
  $('.add_text').show();
}
// 插入文字笔记
function add_txt() {
  var contents = $('#summernote').summernote('code');

  var index =  $('#add_btn').data('list_index');
  // 编辑旧的


  console.log('下标',index);
  if(!isNaN(index)){
    console.log(contents);
    console.log('原始数据为',$('#cs_note_lists .txt_box').eq(index).html());
    // var index = $('#add_btn').data('list_index')+1;
    $('#cs_note_lists .cs-list-txt').eq(index).html(contents);
    $('#summernote').summernote('code','');
    hide_addtxt();
    $('#add_btn').removeAttr('data');
    add_edit_listen();
  }else{
    // 新增的
    // var liststr = '<div class="txt_box">'+contents+'</div>';
    var liststr ='<li class="mui-card mui-table-view-cell cs-slider-li txt_box">'
                +'  <div class="mui-slider-right mui-disabled">'
                +'    <a class="mui-btn mui-btn-red">删除</a>'
                +'  </div>'
                +'  <div class="mui-table mui-slider-handle">'
                +'      <div class="mui-table-cell mui-col-xs-12">'
                +'            <div class="mui-h6 mui-ellipsis-2 cs-list-txt">'+contents+'</div>'
                +'        </div>'
                +'    </div>'
                +'  </li>';


    $('#cs_note_lists').append(liststr);
     $('#summernote').summernote('code','');
    hide_addtxt();
    add_edit_listen();
  }

}
// 插入录音文件
function add_audio () {

}
// 添加图片文件
function add_pic (){

}
// 隐藏编辑框
function hide_addtxt(){
  $('.add_text').hide();
}

//编码
function html_encode(str)
{
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&/g, ">");
    s = s.replace(/</g, "<");
    s = s.replace(/>/g, ">");
    s = s.replace(/ /g, " ");
    s = s.replace(/\'/g, "'");
    s = s.replace(/\"/g, '"');
    s = s.replace(/\n/g, "<br>");
    return s;
}

//解码
function html_decode(str)
{
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/>/g, "&");
    s = s.replace(/</g, "<");
    s = s.replace(/>/g, ">");
    s = s.replace(/ /g, " ");
    s = s.replace(/'/g, "\'");
    s = s.replace(/"/g, "\"");
    s = s.replace(/<br>/g, "\n");
    return s;
}

function html2Escape(sHtml) {

 return sHtml.replace(/[<>&"]/g,function(c){return {'<':'&lt;','>':'&gt;','&':'&amp;','"':'&quot;'}[c];});
}

function escape2Html(str) {
 var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'};
 return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];});
}
function return2Br(str) {
 return str.replace(/\r?\n/g,"<br />");
}

function removeHtmlTab(tab) {
 return tab.replace(/<[^<>]+?>/g,'');//删除所有HTML标签
}
function removegtb(tab) {
 return tab.replace(/[<,>]/g,'');//删除所有HTML标签
}

//Html编码获取Html转义实体
function htmlEncode(value){
  return $('<div/>').text(value).html();
}
//Html解码获取Html实体
function htmlDecode(value){
  return $('<div/>').html(value).text();
}

function save_note (e) {
  var id = $(e).data('id');
  console.log(id);
  var title = $('#title').val();
  var content = html2Escape($('#cs_note_lists').html());
  var descs = $('.cs-list-txt').text().substr(0,100);
  var data ={
    title:title,
    content:content,
    descs:descs,
  }
  if(id){
    data.id = id;
  }
  if(!title||title==''){
    return alert("请输入笔记标题")
  }

  $.ajax({
    url:'/add',
    type:'post',
    dataType:'json',
    data:data,
    success:function (ret){
      console.log(ret);
      alert(ret.ret_msg);
      if(ret.ret_code==0){
        location.href='/';
      }
    },
    error:function(err){
      alert('保存失败');
      console.log(err);
    }

  })

}


function add_edit_listen () {
  $('.txt_box').click(function(){
    var list_index = $(this).index();
    var strs = $(this).find('.cs-list-txt').html();
    console.log('内容为',list_index,strs);
    $('#summernote').summernote('code',strs);
    $('#add_btn').data('list_index',list_index);
    show_addtxt(list_index);
  });
}
